package com.meichis.mcsappframework;

import android.app.Application;
import android.content.Context;

import com.meichis.mcsappframework.executor.MCSExecutorService;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public abstract class BaseApplication extends Application {

    private static Context sContext;

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    public BaseApplication() {
        sContext = this;
    }

    public static Context getContext() {
        if (sContext == null) {
            throw new RuntimeException("Application context is null. Maybe you haven't configured your application.");
        } else {
            return sContext;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initImageLoader(getApplicationContext()); //初始化imageLoader图片加载
    }

    private void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.taskExecutor(MCSExecutorService.getInstance());// 设置自己的线程池
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCache(new UnlimitedDiskCache(context.getExternalCacheDir(), null, new Md5FileNameGenerator()));
        ImageLoader.getInstance().init(config.build());
    }
}
